import {designComponent, getComponentCls, PropType, useClassCache} from "@peryl/react-compose";
import './RuleList.scss';
import {ApplicationConfigurationProvider, Empty, Loading, VirtualList} from "plain-design";
import {iBaseRecord} from "../../modules/base/BaseRecord";
import {PlainBindingNamespace, PlainRuleNamespace} from '@peryl/react-cms-editor';
import {PlainObject} from "@peryl/utils/event";

export const RuleList = designComponent({
  name: 'rule-list',
  props: {
    title: { type: String, required: true },
    data: { type: Array as PropType<PlainObject[]>, required: true },
    loading: { type: Boolean },
  },
  slots: ['operation'],
  scopeSlots: {
    default: (scope: { item: any, index: number, vid: string, vIndex: number }) => true,
  },
  emits: {
    onVerticalScrollBottom: (e: any) => true,
  },
  setup({ props, slots, scopeSlots, event: { emit } }) {

    const configuration = ApplicationConfigurationProvider.inject();

    const classes = useClassCache(() => [
      getComponentCls('rule-list'),
      `rule-list-size-${configuration.value.default.size}`
    ]);

    return () => (
      <div className={classes.value}>
        <div className="rule-list-head">
          <div>{props.title}</div>
          {slots.operation.isExist() && (
            <div className="rule-list-operation">
              {slots.operation()}
            </div>
          )}
        </div>
        <div className="rule-list-body">
          {!!props.data.length && (
            <VirtualList
              data={props.data}
              dynamicSize
              onVerticalScrollBottom={emit.onVerticalScrollBottom}
              v-slots={{
                default: (scope) => {
                  return scopeSlots.default(scope);
                },
              }}
            />
          )}
          {props.loading ? (
            <div className="rule-list-empty">
              <Loading type="delta" status="primary"/>
            </div>
          ) : !props.data.length ?
            (
              <div className="rule-list-empty">
                <Empty noData/>
              </div>
            ) : (null)}
        </div>
      </div>
    );
  },
});

export interface iRuleRecordJsonData {
  ruleMetas?: PlainRuleNamespace.iRuleMeta[];
  ruleVariables?: PlainBindingNamespace.iBindingVariables;
}

/**
 * 规则数据类型
 * @author  韦胜健
 * @date    2024/10/14 19:30
 */
export interface iRuleRecord extends iBaseRecord {
  /*规则显示名称*/
  name: string,
  /*规则备注信息*/
  comments?: string,
  /*规则数据，是一个json数据，为ruleVariables以及ruleMetas，如果这个规则数据是日志子数据，则只有ruleMetas*/
  jsonData?: iRuleRecordJsonData,
}

export type iRuleResultRecord =
  iBaseRecord &
  Omit<PlainRuleNamespace.iRuleRunningRulesResult, 'meta'> &
  {
    parentRuleId: string,
    returnValue: any,
    meta: PlainRuleNamespace.iRuleMeta[] | undefined | null,
  }

/**
 * 将后端返回的rule result record转化成前端展示的 rule result record, 其实就是把flag从字符串转为boolean，后端保存的是字符串0,1，前端用的是true,false
 * @author  韦胜健
 * @date    2024/10/17 21:55
 */
export function formatRuleResultList(result: iRuleResultRecord): void
export function formatRuleResultList(results: iRuleResultRecord[]): void
export function formatRuleResultList(val: any): void {
  if (Array.isArray(val)) {
    val.forEach(i => {
      const result = i as iRuleResultRecord;
      result.flag = typeof result.flag === "boolean" ? result.flag : (result.flag == '1');
    });

  } else {
    const result = val as iRuleResultRecord;
    result.flag = typeof result.flag === "boolean" ? result.flag : (result.flag == '1');
  }
}
